<?php

/**
 * 分页类
 */

namespace Lib;

class Page
{
    private $baseUrl = '';
    private $totalCnt = 0;
    private $pageSize = 20;
    private $pagesign = 'page';
    private $pageCnt = 0;
    private $curPage = 1;
    private $loopCnt = 5;

    public function __construct($params = [])
    {
        if (count($params) > 0) {
            $this->initialize($params);
        }

    }


    /**
     * Initialize Preferences
     *
     * @access    public
     * @param    array    initialization parameters
     * @return    void
     */
    public function initialize($params = [])
    {
        if (count($params) > 0) {
            foreach ($params as $key => $val) {
                if (isset($this->$key)) {
                    $this->$key = $val;
                }
            }
        }
    }


    /**
     * Generate the pagination links
     *
     * @access    public
     * @return    string
     */
    public function createLink()
    {
        if ($this->totalCnt <= 0 || $this->pageSize <= 0) {
            return '';
        }

        $this->pageCnt = ceil($this->totalCnt / $this->pageSize);

        // Current page
        if (!empty($_GET[$this->pagesign])) {
            if ($_GET[$this->pagesign] < 0 || $_GET[$this->pagesign] > $this->pageCnt) {
                $this->curPage = 1;
            } else {
                $this->curPage = $_GET[$this->pagesign];
            }
        }

        $connectSymbol = '?';
        if (strpos($this->baseUrl, '?') !== false) {
            $connectSymbol = '&';
        }

        $urlPrefix = $this->baseUrl . $connectSymbol . $this->pagesign . '=';
        $html      = '<ul class="pagination pull-right">';

        // Pre page
        if ($this->curPage == 1) {
            $html .= '<li class="disabled"><a >&laquo;</a></li>';
        } else {
            $preLink = $this->baseUrl . $connectSymbol . $this->pagesign . '=' . ($this->curPage - 1);
            $html    .= '<li><a href="' . $preLink . '">&laquo;</a></li>';
        }

        $beforeCnt = $this->curPage - $this->loopCnt;
        if ($beforeCnt > 1) {
            $url  = $urlPrefix . '1';
            $html .= '<li><a href="' . $url . '" >1</a></li>';
            if ($beforeCnt - 1 != 1) {
                $html .= '<li class="disabled"><a >...</a></li>';
            }
        } else {
            $beforeCnt = 1;
        }

        $afterCnt = $this->curPage + $this->loopCnt;
        if ($afterCnt >= $this->pageCnt) {
            $afterCnt = $this->pageCnt;
        }
        for ($i = $beforeCnt; $i <= $afterCnt; $i++) {
            $url = $urlPrefix . $i;
            if ($i == $this->curPage) {
                $html .= '<li class="active"><a href="' . $url . '" >' . $i . '</a></li>';
            } else {
                $html .= '<li><a href="' . $url . '">' . $i . '</a></li>';
            }
        }
        if ($afterCnt < $this->pageCnt) {
            $url = $urlPrefix . $this->pageCnt;
            if ($afterCnt + 1 != $this->pageCnt) {
                $html .= '<li class="disabled"><a >...</a></li>';
            }
            $html .= '<li><a href="' . $url . '" >' . $this->pageCnt . '</a></li>';

        }

        // Next page
        if ($this->curPage == $this->pageCnt) {
            $html .= '<li class="disabled"><a >&raquo;</a></li>';
        } else {
            $nextLink = $this->baseUrl . $connectSymbol . $this->pagesign . '=' . ($this->curPage + 1);
            $html     .= '<li><a href="' . $nextLink . '">&raquo;</a></li>';
        }

        $html .= '<li style="margin-left: 10px;"> <label class="btn btn-default">共 '.$this->totalCnt.' 条记录</label></li>';

        $html .= '</ur>';

        return $html;
    }

}
